/**
 * RenweiWu
 * PubOrganzationService.java
 * 2017年8月24日
 * PubOrganzationService
 * TODO
 * 
 */
package com.etrans.web.gps.business.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.base.domain.PubOrganzation;
import com.etrans.web.gps.business.base.domain.PubOrganzationvo;
import com.etrans.web.gps.common.page.PageVo;

/**
 * @Description:TODO
 * @author RenweiWu
 * @date 2017年8月24日
 * @ClassName:  PubOrganzationService  
 * @param:   
 */

@Service
public class PubOrganzationService extends BaseService {
	
	public boolean addPubOrganzation(PubOrganzation pub){
		boolean result=false;
		this.insert("pubOrganzationMapper.addPubOrganzation", pub);	
		// 拼接8位fullID
		String organzationString = String.valueOf(pub.getId()).trim();
		if(pub.getParent_orgn()==null||"".equals(pub.getParent_orgn())){
			pub.setParent_orgn(pub.getOrgnName());
		}else{
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("id",pub.getParent_orgn());
			pub.setParent_orgn(this.selectOne("pubOrganzationQuery.queryorgnById",map));
		}
		if (organzationString.length() < 8) {
			while (organzationString.length() < 8) {
				organzationString = "0" + organzationString;
			}
		}
		// 获取父节点信息,设置fullID，fullName
		if (pub.getParentid() != 0) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", pub.getParentid());
			PubOrganzationvo perentOrganzation = (PubOrganzationvo) this.selectOne("pubOrganzationQuery.queryparentById",map);
			pub.setFullid(perentOrganzation.getFullid() + "." + organzationString);
		} else {
			pub.setFullid(organzationString);
		}
		// 最后更新FullID,FullName回数据库	
		this.update("pubOrganzationMapper.updatePubOrganzation", pub);
		result=true;
		return result;
	}
	
	public PageVo getPubOrganzation(Map<String,Object> map){
		PageVo pagevo = new PageVo();
		List<PubOrganzation> list = selectList("pubOrganzationQuery.querypage",map);
		int count = selectOne("pubOrganzationQuery.querycount",map);
		pagevo.setTotal(count);
		pagevo.setRows(list);
		pagevo.setPage((Integer) map.get("page"));
		return pagevo;
	}
	
	@SuppressWarnings("unchecked")
	public List<HashMap<String, Object>> queryFullUnitTree(HashMap<String, Object> paramsMap) {
		@SuppressWarnings("rawtypes")
		List list = selectList("pubOrganzationQuery.queryFullUnitTree", paramsMap);
		return list;
	}
	
	public PubOrganzationvo  queryOneById(int id){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("id", id);
		return this.selectOne("pubOrganzationQuery.queryparentById",map);
	}
	
	public boolean updatePubOrganzation(PubOrganzation pub){
		boolean result=false;
		// 拼接8位fullID
		String organzationString = String.valueOf(pub.getId()).trim();
		if (organzationString.length() < 8) {
			while (organzationString.length() < 8) {
				organzationString = "0" + organzationString;
			}
		}
		// 获取父节点信息,设置fullID，fullName
		if (pub.getParentid() != 0) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", pub.getParentid());
			PubOrganzationvo perentOrganzation = (PubOrganzationvo) this.selectOne("pubOrganzationQuery.queryparentById",map);
			pub.setFullid(perentOrganzation.getFullid() + "." + organzationString);
		} else {
			pub.setFullid(organzationString);
		}
		// 最后更新FullID,FullName回数据库	
		this.update("pubOrganzationMapper.updatePubOrganzation", pub);
		result=true;
		return result;
	}
	
	public boolean deleteOrganzation(List<Integer> sid){
		boolean result = false;
			Map<String,Object> map =new HashMap<String,Object>();
			map.put("id",sid);
			this.delete("pubOrganzationMapper.deleteOrganzation",map);
			result =true;
		return result;
	}
	public boolean checkOrgnName(String name,String value){
		boolean result = false;
		Map<String,Object> map =new HashMap<String,Object>();
		map.put(name,value);
		int count = (int)selectOne("pubOrganzationQuery.querycount",map);
		if(count>0){
			result=true;
		}
		return result;
	}	
}
